package cn.fws.meap.community.service.impl;

import java.util.ArrayList;
import java.util.List;

import cn.fws.meap.community.enums.DetailsTypeEnum;
import cn.fws.meap.community.service.DetailsUtils;
import cn.fws.meap.community.vo.Details;
import cn.fws.meap.community.vo.InformationListVo;
import cn.fws.meap.utils.QiNiuUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.fws.framework.utils.ConvertUtils;
import cn.fws.framework.utils.UUIDUtils;

import cn.fws.meap.community.mapper.InformationMapper;
import cn.fws.meap.community.entity.Information;
import cn.fws.meap.community.service.IInformationService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 资讯 服务层实现
 *
 * @author chenkh
 * @date 2019-03-07
 */
@Service
@Transactional
public class InformationServiceImpl implements IInformationService {

	@Autowired
	private InformationMapper informationMapper;
	@Autowired
	private DetailsUtils<Information> detailsUtil;

	/**
     * 查询资讯信息
     *
     * @param id 资讯ID
     * @return 资讯信息
     */
    @Override
	public Information selectById(String id) {
	    return informationMapper.selectById(id);
	}

	/**
     * 查询资讯列表
     *
     * @param information 资讯信息
     * @return 资讯集合
     */
	@Override
	public List<InformationListVo> selectList(Information information)	{
		List<InformationListVo> infoList = informationMapper.selectList(information);
		for (InformationListVo informations : infoList) {
			informations.setInfoMsg(QiNiuUtil.getFileMsg(informations.getInfoMsg()));
		}
		//传入资讯列表,返回资讯详情列表
		return infoList;
	}

	@Override
	public List<Details<Information>> selectListNew() {
		List<Information> infoList = informationMapper.selectListNew();
		//传入资讯列表,返回资讯详情列表
		return getDetailsList(infoList);
	}

	/**
     * 新增资讯
     * 
     * @param information 资讯信息
     * @return 结果
     */
	@Override
	public int insert(Information information) {
		// 设置主键
		information.setId(UUIDUtils.getUUID());
		// 设置创建时间
		information.setCreateTime(new java.util.Date());
	    return informationMapper.insert(information);
	}
	
	/**
     * 修改资讯
     * 
     * @param information 资讯信息
     * @return 结果
     */
	@Override
	public int update(Information information) {
		// 设置修改时间
		information.setUpdateTime(new java.util.Date());
	    return informationMapper.update(information);
	}

	/**
     * 删除资讯对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteByIds(String ids) {
		return informationMapper.deleteByIds(ConvertUtils.toStrArray(ids));
	}

	/**
	 * 传入资讯列表,返回资讯详情列表
	 * @param infoList 资讯列表
	 * @return 资讯详情信息列表
	 */
	private List<Details<Information>> getDetailsList(List<Information> infoList){
		List<Details<Information>> detailsList = new ArrayList<>();
		for (Information info : infoList) {
			//获取资讯ID
			String infoId = info.getId();
			//获取openid
			String openid = info.getOpenid();
			Details<Information> details = detailsUtil.getDetails(infoId, openid, DetailsTypeEnum.IS_INFO.getCode());
			//设置资讯
			details.setT(info);
			//将实体类添加到集合
			detailsList.add(details);
		}

		return detailsList;

	}
	
}
